import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:xim/common/encrypt_image_provider.dart';
import 'package:xim/router/index.dart';

/// 图片预览页面
class PhotoViewPage extends StatefulWidget {

  final double minScale;
  final double maxScale;
  dynamic initialScale;
  final MediaView mediaView;

  PhotoViewPage({
    Key? key,
    this.maxScale = 2,
    this.minScale = 0.5,
    this.initialScale ,
    required this.mediaView
  }) : super(key: key);

  @override
  State<PhotoViewPage> createState() => _PhotoViewPageState();
}

class _PhotoViewPageState extends State<PhotoViewPage> {

  @override
  void initState() {
    super.initState();
    // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.bottom]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      backgroundColor: Colors.black,
      body: PhotoViewGallery.builder(itemCount: 1, builder: (context, index) {
        return PhotoViewGalleryPageOptions(
          imageProvider: widget.mediaView.plaintextAESKey==null
              ? getImageProvider(widget.mediaView.gfsDownloadPath)
              // : EncryptNetWorkImageProvider(widget.mediaView.gfsDownloadPath, widget.mediaView.plaintextAESKey!),
              : NetworkImage(widget.mediaView.gfsDownloadPath),
          maxScale:  PhotoViewComputedScale.contained * widget.maxScale,
          minScale: PhotoViewComputedScale.contained * widget.minScale,
          initialScale: PhotoViewComputedScale.contained,
          heroAttributes: const PhotoViewHeroAttributes(tag: ""),
        );
      }),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}

ImageProvider getImageProvider(String url) {
  if (url.startsWith('http')) {
    return CachedNetworkImageProvider(url);
  } else if (url.startsWith('assets')) {
    return AssetImage(url);
  } else {
    return FileImage(
      File(url),
    );
  }
}

